/**
 * 
 */
package com.htht.tourism.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.htht.tourism.domain.Response;
import com.htht.tourism.domain.Shopping;
import com.htht.tourism.domain.link.ShoppingParam;
import com.htht.tourism.intf.ShoppingService;
import com.htht.tourism.utils.ValidatorResultHandler;

/**
 * @author wuqiwei
 *
 */
@Controller
@ResponseBody
@RequestMapping("/shopping")
public class ShoppingController {
	@Resource
	private ShoppingService shoppingService;
	
	/**
	 * 查询全部商品信息
	 * @author:wuqiwei
	 * @param @param shopping
	 * @param @param result
	 * @param @return
	 * @date:2017年3月13日上午9:55:13
	 */
	@RequestMapping(value = "/getAll", method = RequestMethod.POST)
	public Response queryAllUser(@RequestBody @Validated ShoppingParam shopping,
			BindingResult result) {
		Response retVal = ValidatorResultHandler.handle(result);
		if (retVal == null) {
			Map map = this.shoppingService.queryShopping(shopping);
			if (map.size() > 0) {
				return Response.getInstance().success(map);
			} else {
				return Response.getInstance().failure("查询失败！");
			}
		} else {
			return retVal;
		}
	}
	/**
	 * 添加商品信息
	 * @author:wuqiwei
	 * @param @param shopping
	 * @param @param result
	 * @param @return
	 * @date:2017年3月13日下午3:28:31
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response add(@RequestBody @Validated ShoppingParam shopping,
			BindingResult result) {
		Response retVal = ValidatorResultHandler.handle(result);
		if (retVal == null) {
			int a = this.shoppingService.add(shopping);
			if (a > 0) {
				return Response.getInstance().success(a);
			} else {
				return Response.getInstance().failure("添加失败！");
			}
		} else {
			return retVal;
		}
	}
	/**
	 * 删除商品信息
	 * @author:wuqiwei
	 * @param @param shopping
	 * @param @param result
	 * @param @return
	 * @date:2017年3月13日下午4:06:07
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Response del(@RequestBody @Validated ShoppingParam shopping,
			BindingResult result) {
		Response retVal = ValidatorResultHandler.handle(result);
		if (retVal == null) {
			int a = this.shoppingService.del(shopping);
			if (a > 0) {
				return Response.getInstance().success(a);
			} else {
				return Response.getInstance().failure("删除失败！");
			}
		} else {
			return retVal;
		}
	}
	/**
	 * 修改商品信息
	 * @author:wuqiwei
	 * @param @param shopping
	 * @param @param result
	 * @param @return
	 * @date:2017年3月13日下午4:28:38
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response update(@RequestBody @Validated ShoppingParam shopping,
			BindingResult result) {
		Response retVal = ValidatorResultHandler.handle(result);
		if (retVal == null) {
			int a = this.shoppingService.update(shopping);
			if (a > 0) {
				return Response.getInstance().success(a);
			} else {
				return Response.getInstance().failure("修改失败！");
			}
		} else {
			return retVal;
		}
	}
	/**
	 * 根据关键字查询商品信息
	 * @author:wuqiwei
	 * @param @param shopping
	 * @param @param result
	 * @param @return
	 * @date:2017年3月14日上午9:27:35
	 */
	@RequestMapping(value = "/find", method = RequestMethod.POST)
	public Response find(@RequestBody @Validated ShoppingParam shopping,
			BindingResult result) {
		Response retVal = ValidatorResultHandler.handle(result);
		if (retVal == null) {
			List<Shopping> list = this.shoppingService.find(shopping);
			if (list.size() > 0) {
				return Response.getInstance().success(list);
			} else {
				return Response.getInstance().failure("查询失败！");
			}
		} else {
			return retVal;
		}
	}
}
